<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
  <meta name="theme-color" content="#222">
  <meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

  <link rel="stylesheet" href="/css/main.css">



  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

  <script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {
      "hostname": "holidaypenguin.gitee.io",
      "root": "/",
      "images": "/images",
      "scheme": "Mist",
      "version": "8.2.2",
      "exturl": false,
      "sidebar": {
        "position": "right",
        "display": "always",
        "padding": 18,
        "offset": 12
      },
      "copycode": true,
      "bookmark": {
        "enable": false,
        "color": "#222",
        "save": "auto"
      },
      "fancybox": false,
      "mediumzoom": false,
      "lazyload": false,
      "pangu": false,
      "comments": {
        "style": "tabs",
        "active": null,
        "storage": true,
        "lazyload": false,
        "nav": null
      },
      "motion": {
        "enable": true,
        "async": true,
        "transition": {
          "post_block": "fadeIn",
          "post_header": "fadeInDown",
          "post_body": "fadeInDown",
          "coll_header": "fadeInLeft",
          "sidebar": "slideUpIn"
        }
      },
      "prism": false,
      "i18n": {
        "placeholder": "搜索...",
        "empty": "没有找到任何搜索结果：${query}",
        "hits_time": "找到 ${hits} 个搜索结果（用时 ${time} 毫秒）",
        "hits": "找到 ${hits} 个搜索结果"
      },
      "path": "/search.xml",
      "localsearch": {
        "enable": true,
        "trigger": "auto",
        "top_n_per_article": 1,
        "unescape": false,
        "preload": false
      }
    };
  </script>
  <meta property="og:type" content="article">
  <meta property="og:title" content="JavsScript ES6&amp;ES2015的新特性">
  <meta property="og:url" content="https://holidaypenguin.gitee.io/blob/2019-04-17-new-features-of-javsscript-es6-&-es2015/index.html">
  <meta property="og:site_name" content="HolidayPenguin">
  <meta property="og:locale" content="zh_CN">
  <meta property="article:published_time" content="2019-04-17T12:12:31.000Z">
  <meta property="article:modified_time" content="2019-04-17T12:12:31.000Z">
  <meta property="article:author" content="holidaypenguin">
  <meta property="article:tag" content="JavaScript">
  <meta property="article:tag" content="Ecma">
  <meta property="article:tag" content="ECMAScript">
  <meta property="article:tag" content="ES6">
  <meta property="article:tag" content="ES2015">
  <meta property="article:tag" content="ECMAScript 2015">
  <meta name="twitter:card" content="summary">


  <link rel="canonical" href="https://holidaypenguin.gitee.io/blob/2019-04-17-new-features-of-javsscript-es6-&-es2015/">


  <script class="page-configurations">
    // https://hexo.io/docs/variables.html
    CONFIG.page = {
      sidebar: "",
      isHome: false,
      isPost: true,
      lang: 'zh-CN'
    };
  </script>
  <title>JavsScript ES6&ES2015的新特性 | HolidayPenguin</title>





  <noscript>
    <style>
      body {
        margin-top: 2rem;
      }

      .use-motion .menu-item,
      .use-motion .sidebar,
      .use-motion .post-block,
      .use-motion .pagination,
      .use-motion .comments,
      .use-motion .post-header,
      .use-motion .post-body,
      .use-motion .collection-header {
        visibility: visible;
      }

      .use-motion .header,
      .use-motion .site-brand-container .toggle,
      .use-motion .footer {
        opacity: initial;
      }

      .use-motion .site-title,
      .use-motion .site-subtitle,
      .use-motion .custom-logo-image {
        opacity: initial;
        top: initial;
      }

      .use-motion .logo-line {
        transform: scaleX(1);
      }

      .search-pop-overlay,
      .sidebar-nav {
        display: none;
      }

      .sidebar-panel {
        display: block;
      }
    </style>
  </noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner">
        <div class="site-brand-container">
          <div class="site-nav-toggle">
            <div class="toggle" aria-label="切换导航栏" role="button">
              <span class="toggle-line"></span>
              <span class="toggle-line"></span>
              <span class="toggle-line"></span>
            </div>
          </div>

          <div class="site-meta">

            <a href="/" class="brand" rel="start">
              <i class="logo-line"></i>
              <h1 class="site-title">HolidayPenguin</h1>
              <i class="logo-line"></i>
            </a>
          </div>

          <div class="site-nav-right">
            <div class="toggle popup-trigger">
              <i class="fa fa-search fa-fw fa-lg"></i>
            </div>
          </div>
        </div>



        <nav class="site-nav">
          <ul class="main-menu menu">
            <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
            <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
            <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
            <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
            <li class="menu-item menu-item-search">
              <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
              </a>
            </li>
          </ul>
        </nav>



        <div class="search-pop-overlay">
          <div class="popup search-popup">
            <div class="search-header">
              <span class="search-icon">
                <i class="fa fa-search"></i>
              </span>
              <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input">
              </div>
              <span class="popup-btn-close" role="button">
                <i class="fa fa-times-circle"></i>
              </span>
            </div>
            <div class="search-result-container no-result">
              <div class="search-result-icon">
                <i class="fa fa-spinner fa-pulse fa-5x"></i>
              </div>
            </div>

          </div>
        </div>

      </div>


      <div class="toggle sidebar-toggle" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
      </div>

      <aside class="sidebar">

        <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
          <ul class="sidebar-nav">
            <li class="sidebar-nav-toc">
              文章目录
            </li>
            <li class="sidebar-nav-overview">
              站点概览
            </li>
          </ul>

          <div class="sidebar-panel-container">
            <!--noindex-->
            <div class="post-toc-wrap sidebar-panel">
              <div class="post-toc animated">
                <ol class="nav">
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Arrow-functions%EF%BC%88%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0%EF%BC%89"><span class="nav-number">1.</span> <span class="nav-text">Arrow functions（箭头函数）</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Promise"><span class="nav-number">2.</span> <span class="nav-text">Promise</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#let-%E5%92%8C-const"><span class="nav-number">3.</span> <span class="nav-text">let 和 const</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Classes%EF%BC%88%E7%B1%BB%EF%BC%89"><span class="nav-number">4.</span> <span class="nav-text">Classes（类）</span></a>
                    <ol class="nav-child">
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#constructor-NaN"><span class="nav-number">4.1.</span> <span class="nav-text">constructor</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#extends-amp-super"><span class="nav-number">4.2.</span> <span class="nav-text">extends &amp; super</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%96%E5%80%BC%E5%87%BD%E6%95%B0%EF%BC%88getter%EF%BC%89%E5%92%8C%E5%AD%98%E5%80%BC%E5%87%BD%E6%95%B0%EF%BC%88setter%EF%BC%89"><span class="nav-number">4.3.</span> <span class="nav-text">取值函数（getter）和存值函数（setter）</span></a></li>
                    </ol>
                  </li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Modules%EF%BC%88%E6%A8%A1%E5%9D%97%EF%BC%89"><span class="nav-number">5.</span> <span class="nav-text">Modules（模块）</span></a>
                    <ol class="nav-child">
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#import"><span class="nav-number">5.1.</span> <span class="nav-text">import</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#export"><span class="nav-number">5.2.</span> <span class="nav-text">export</span></a></li>
                    </ol>
                  </li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Template-string%EF%BC%88%E6%A8%A1%E6%9D%BF%E5%AD%97%E7%AC%A6%E4%B8%B2%EF%BC%89"><span class="nav-number">6.</span> <span class="nav-text">Template string（模板字符串）</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Default-parameters%EF%BC%88%E9%BB%98%E8%AE%A4%E5%8F%82%E6%95%B0%EF%BC%89"><span class="nav-number">7.</span> <span class="nav-text">Default parameters（默认参数）</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#The-spread-operator%EF%BC%88%E5%B1%95%E5%BC%80%E6%93%8D%E4%BD%9C%E7%AC%A6%EF%BC%89"><span class="nav-number">8.</span> <span class="nav-text">The spread operator（展开操作符）</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Destructuring-assignments%EF%BC%88%E8%A7%A3%E6%9E%84%E8%B5%8B%E5%80%BC%EF%BC%89"><span class="nav-number">9.</span> <span class="nav-text">Destructuring assignments（解构赋值）</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Enhanced-object-literals%EF%BC%88%E5%A2%9E%E5%BC%BA%E7%9A%84%E5%AF%B9%E8%B1%A1%E5%AD%97%E9%9D%A2%E9%87%8F%EF%BC%89"><span class="nav-number">10.</span> <span class="nav-text">Enhanced object literals（增强的对象字面量）</span></a>
                    <ol class="nav-child">
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B1%9E%E6%80%A7%E7%9A%84%E7%AE%80%E6%B4%81%E6%A0%87%E8%AF%86%E6%B3%95"><span class="nav-number">10.1.</span> <span class="nav-text">属性的简洁标识法</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B1%9E%E6%80%A7%E5%90%8D%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-number">10.2.</span> <span class="nav-text">属性名表达式</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#super-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="nav-number">10.3.</span> <span class="nav-text">super 关键字</span></a></li>
                    </ol>
                  </li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Generators"><span class="nav-number">11.</span> <span class="nav-text">Generators</span></a>
                    <ol class="nav-child">
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9D%E6%AD%A5%E4%BA%86%E8%A7%A3"><span class="nav-number">11.1.</span> <span class="nav-text">初步了解</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E4%B8%AA%E6%9B%B4%E9%AB%98%E7%BA%A7%E7%9A%84%E4%BE%8B%E5%AD%90"><span class="nav-number">11.2.</span> <span class="nav-text">一个更高级的例子</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E7%9A%84%E5%BA%94%E7%94%A8"><span class="nav-number">11.3.</span> <span class="nav-text">异步编程的应用</span></a></li>
                    </ol>
                  </li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#for-of-%E5%BE%AA%E7%8E%AF"><span class="nav-number">12.</span> <span class="nav-text">for..of 循环</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#Map-%E5%92%8C-Set"><span class="nav-number">13.</span> <span class="nav-text">Map 和 Set</span></a></li>
                </ol>
                </li>
                <li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number"></span> <span class="nav-text">参考</span></a>
              </div>
            </div>
            <!--/noindex-->

            <div class="site-overview-wrap sidebar-panel">
              <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
                <p class="site-author-name" itemprop="name">holidaypenguin</p>
                <div class="site-description" itemprop="description"></div>
              </div>
              <div class="site-state-wrap site-overview-item animated">
                <nav class="site-state">
                  <div class="site-state-item site-state-posts">
                    <a href="/archives/">

                      <span class="site-state-item-count">138</span>
                      <span class="site-state-item-name">日志</span>
                    </a>
                  </div>
                  <div class="site-state-item site-state-categories">
                    <a href="/categories/">

                      <span class="site-state-item-count">26</span>
                      <span class="site-state-item-name">分类</span></a>
                  </div>
                  <div class="site-state-item site-state-tags">
                    <a href="/tags/">

                      <span class="site-state-item-count">234</span>
                      <span class="site-state-item-name">标签</span></a>
                  </div>
                </nav>
              </div>
              <div class="links-of-author site-overview-item animated">
                <span class="links-of-author-item">
                  <a href="https://github.com/holidaypenguin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;holidaypenguin" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
                </span>
                <span class="links-of-author-item">
                  <a href="mailto:songshipeng2016@gmail.com" title="E-Mail → mailto:songshipeng2016@gmail.com" rel="noopener" target="_blank"><i class="envelope fa-fw"></i>E-Mail</a>
                </span>
              </div>



            </div>
          </div>
        </div>
      </aside>
      <div class="sidebar-dimmer"></div>


    </header>


    <div class="back-to-top" role="button">
      <i class="fa fa-arrow-up"></i>
      <span>0%</span>
    </div>

    <noscript>
      <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
    </noscript>


    <div class="main-inner post posts-expand">





      <div class="post-block">



        <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
          <link itemprop="mainEntityOfPage" href="https://holidaypenguin.gitee.io/blob/2019-04-17-new-features-of-javsscript-es6-&-es2015/">

          <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
            <meta itemprop="image" content="/images/avatar.gif">
            <meta itemprop="name" content="holidaypenguin">
            <meta itemprop="description" content="">
          </span>

          <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
            <meta itemprop="name" content="HolidayPenguin">
          </span>
          <header class="post-header">
            <h1 class="post-title" itemprop="name headline">
              JavsScript ES6&ES2015的新特性
            </h1>

            <div class="post-meta-container">
              <div class="post-meta">
                <span class="post-meta-item">
                  <span class="post-meta-item-icon">
                    <i class="far fa-calendar"></i>
                  </span>
                  <span class="post-meta-item-text">发表于</span>

                  <time title="创建时间：2019-04-17 20:12:31" itemprop="dateCreated datePublished" datetime="2019-04-17T20:12:31+08:00">2019-04-17</time>
                </span>
                <span class="post-meta-item">
                  <span class="post-meta-item-icon">
                    <i class="far fa-folder"></i>
                  </span>
                  <span class="post-meta-item-text">分类于</span>
                  <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                    <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
                  </span>
                </span>


                <span id="/blob/2019-04-17-new-features-of-javsscript-es6-&-es2015/" class="post-meta-item leancloud_visitors" data-flag-title="JavsScript ES6&ES2015的新特性" title="阅读次数">
                  <span class="post-meta-item-icon">
                    <i class="far fa-eye"></i>
                  </span>
                  <span class="post-meta-item-text">阅读次数：</span>
                  <span class="leancloud-visitors-count"></span>
                </span>
              </div>

              <div class="post-description">
                <!-- more -->
              </div>
            </div>
          </header>




          <div class="post-body" itemprop="articleBody">
            <p>写在前面的话标准非实际情况。</p>
            <p>ECMAScript 2015，也称为 ES6 或者 ES2015，是 ECMAScript 标准的基础版本。</p>
            <p>在标准版本 <a target="_blank" rel="noopener" href="http://lzw.me/pages/ecmascript/">ECMAScript 5.1</a> 发布4年后发布，还有一个重要的变化是 ECMAScript 标准版本的命名从版本号命名改为了根据发布年号命名。</p>
            <p>所以它不应该被命名为 ES6（尽管每个人都这样称呼它），更加准确的命名应该是 ES2015 。</p>
            <p>由于 ES5.1 和 ES6 之间已经过了很长时间，因此 ES6 带来了很多重大的变化，一些重要的新功能以及在开发 JavaScript 程序时建议的最佳实践。 </p>
            <p>ES2015 最重要的变化包括：</p>
            <ul>
              <li>Arrow functions（箭头函数）</li>
              <li>Promise</li>
              <li>Generators</li>
              <li>let 和 const</li>
              <li>Classes（类）</li>
              <li>Modules（模块）</li>
              <li>Template string（模板字符串）</li>
              <li>Default parameters（默认参数）</li>
              <li>The spread operator（展开操作符）</li>
              <li>Destructuring assignments（解构分配）</li>
              <li>Enhanced object literals（增强的对象字面量）</li>
              <li>for..of 循环</li>
              <li>Map 和 Set</li>
            </ul>
            <h2 id="Arrow-functions（箭头函数）"><a href="#Arrow-functions（箭头函数）" class="headerlink" title="Arrow functions（箭头函数）"></a>Arrow functions（箭头函数）</h2>
            <ul>
              <li>
                <p>箭头函数作为一个传统函数的简写形式他改变了代码的外观和工作方式，但是这种改变是受欢迎的。</p>
                <figure class="highlight js">
                  <table>
                    <tr>
                      <td class="gutter">
                        <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre>
                      </td>
                      <td class="code">
                        <pre><span class="line"><span class="keyword">var</span> f = <span class="function"><span class="params">v</span> =&gt;</span> v;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 等同于</span></span><br><span class="line"><span class="keyword">var</span> f = <span class="function"><span class="keyword">function</span> (<span class="params">v</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> v;</span><br><span class="line">&#125;;</span><br></pre>
                      </td>
                    </tr>
                  </table>
                </figure>
              </li>
              <li>
                <p>如果箭头函数不需要参数或需要多个参数，就使用一个圆括号代表参数部分。</p>
                <figure class="highlight js">
                  <table>
                    <tr>
                      <td class="gutter">
                        <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre>
                      </td>
                      <td class="code">
                        <pre><span class="line"><span class="keyword">var</span> f = <span class="function">() =&gt;</span> <span class="number">5</span>;</span><br><span class="line"><span class="comment">// 等同于</span></span><br><span class="line"><span class="keyword">var</span> f = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="number">5</span> &#125;;</span><br></pre>
                      </td>
                    </tr>
                  </table>
                </figure>
              </li>
              <li>
                <p>如果箭头函数的代码块部分多于一条语句，就要使用大括号将它们括起来，并且使用return语句返回。</p>
                <figure class="highlight js">
                  <table>
                    <tr>
                      <td class="gutter">
                        <pre><span class="line">1</span><br></pre>
                      </td>
                      <td class="code">
                        <pre><span class="line"><span class="keyword">var</span> sum = <span class="function">(<span class="params">num1, num2</span>) =&gt;</span> &#123; <span class="keyword">return</span> num1 + num2; &#125;</span><br></pre>
                      </td>
                    </tr>
                  </table>
                </figure>
              </li>
              <li>
                <p>由于大括号被解释为代码块，所以如果箭头函数直接返回一个对象，必须在对象外面加上括号，否则会报错。</p>
                <figure class="highlight js">
                  <table>
                    <tr>
                      <td class="gutter">
                        <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                      </td>
                      <td class="code">
                        <pre><span class="line"><span class="comment">// 报错</span></span><br><span class="line"><span class="keyword">let</span> getTempItem = <span class="function"><span class="params">id</span> =&gt;</span> &#123; <span class="attr">id</span>: id, <span class="attr">name</span>: <span class="string">&quot;Temp&quot;</span> &#125;;</span><br><span class="line"><span class="comment">// 不报错</span></span><br><span class="line"><span class="keyword">let</span> getTempItem = <span class="function"><span class="params">id</span> =&gt;</span> (&#123; <span class="attr">id</span>: id, <span class="attr">name</span>: <span class="string">&quot;Temp&quot;</span> &#125;);</span><br></pre>
                      </td>
                    </tr>
                  </table>
                </figure>
              </li>
              <li>
                <p>新的<code>this</code>的作用域<br>函数体内的<code>this</code>对象，就是定义时所在的对象，而不是使用时所在的对象。如果不明白可以查看<a target="_blank" rel="noopener" href="https://www.html.cn/archives/6959">ES2015 中的箭头函数和词法 this</a></p>
              </li>
            </ul>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/function#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Promise"><a href="#Promise" class="headerlink" title="Promise"></a>Promise</h2>
            <p><code>Promise</code>的引入是为了消除回调地狱，简单说<code>Promise</code>就是一个容器，里面保存着某个未来才会结束的事件（通常是一个异步操作）的结果。</p>
            <p><code>Promise</code>对象有以下两个特点。</p>
            <ul>
              <li>对象的状态不受外界影响。<code>Promise</code>对象代表一个异步操作，有三种状态：<code>pending</code>（进行中）、<code>fulfilled</code>（已成功）和<code>rejected</code>（已失败）。</li>
              <li>一旦状态改变，就不会再变，任何时候都可以得到这个结果。<code>Promise</code>对象的状态改变，只有两种可能：从<code>pending</code>变为<code>fulfilled</code>和从<code>pending</code>变为<code>rejected</code>。</li>
            </ul>
            <p>回调代码</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;I promised to run after 1s&#x27;</span>)</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;I promised to run after 2s&#x27;</span>)</span><br><span class="line">  &#125;, <span class="number">1000</span>)</span><br><span class="line">&#125;, <span class="number">1000</span>)</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p><code>Promise</code>代码</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> wait = <span class="function">() =&gt;</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(resolve, <span class="number">1000</span>)</span><br><span class="line">&#125;)</span><br><span class="line">wait().then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;I promised to run after 1s&#x27;</span>)</span><br><span class="line">  <span class="keyword">return</span> wait()</span><br><span class="line">&#125;)</span><br><span class="line">.then(<span class="function">() =&gt;</span> <span class="built_in">console</span>.log(<span class="string">&#x27;I promised to run after 2s&#x27;</span>))</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/promise">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="let-和-const"><a href="#let-和-const" class="headerlink" title="let 和 const"></a>let 和 const</h2>
            <p><code>let</code>命令，用来声明变量。它的用法类似于<code>var</code>，但是所声明的变量，只在<code>let</code>命令所在的代码块内有效。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">&#123;</span><br><span class="line">  <span class="keyword">let</span> a = <span class="number">10</span>;</span><br><span class="line">  <span class="keyword">var</span> b = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">a <span class="comment">// ReferenceError: a is not defined.</span></span><br><span class="line">b <span class="comment">// 1</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>最典型的例子就是在<code>for</code>循环中输出<code>i</code>，下面先看一下使用var声明变量的情况：</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">var</span> a = [];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">  a[i] = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(i);</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line">a[<span class="number">6</span>](); <span class="comment">// 10</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>变量<code>i</code>是<code>var</code>命令声明的，在全局范围内都有效，所以全局只有一个变量<code>i</code>。每一次循环，变量<code>i</code>的值都会发生改变，而循环内被赋给数组<code>a</code>的函数内部的<code>console.log(i)</code>，里面的<code>i</code>指向的就是全局的i。最后也就是10。</p>
            <p>如果使用let，声明的变量仅在<strong>块级作用域</strong>内有效，最后输出的是 6。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">var</span> a = [];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</span><br><span class="line">  a[i] = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(i);</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line">a[<span class="number">6</span>](); <span class="comment">// 6</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p><code>let</code>还有 <strong>不存在变量提升</strong>、<strong>暂时性死区</strong>的特性，而且<strong>不允许重复声明</strong>。</p>
            <p><code>const</code>具有<code>let</code>的所有特性，同时其所声明的变量是只读的，如果是一个数组或者对象（应该叫复合数据类型）更改各自的属性还是可以的。</p>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/let">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Classes（类）"><a href="#Classes（类）" class="headerlink" title="Classes（类）"></a>Classes（类）</h2>
            <p>生成实例对象的传统方法是通过构造函数。下面是一个例子。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Point</span>(<span class="params">x, y</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.x = x;</span><br><span class="line">  <span class="built_in">this</span>.y = y;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Point.prototype.toString = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">&#x27;(&#x27;</span> + <span class="built_in">this</span>.x + <span class="string">&#x27;, &#x27;</span> + <span class="built_in">this</span>.y + <span class="string">&#x27;)&#x27;</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> Point(<span class="number">1</span>, <span class="number">2</span>);</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>有了<code>Class</code>之后我们可以让代码更简单一点。看下面的例子。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Point</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params">x, y</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.x = x;</span><br><span class="line">    <span class="built_in">this</span>.y = y;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="title">toString</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;(&#x27;</span> + <span class="built_in">this</span>.x + <span class="string">&#x27;, &#x27;</span> + <span class="built_in">this</span>.y + <span class="string">&#x27;)&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> Point(<span class="number">1</span>, <span class="number">2</span>);</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>上面这两段代码的输出是完全一样的，而且<code>class</code>完全可以看作构造函数的另一种写法。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">typeof</span> Point <span class="comment">// &quot;function&quot;</span></span><br><span class="line">Point === Point.prototype.constructor <span class="comment">// true</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p><code>class</code>定义的类不能作为方式使用，否则会报错。</p>
            <p>类的实例共用同一个原型对象</p>
            <h3 id="constructor-NaN"><a href="#constructor-NaN" class="headerlink" title="constructor"></a>constructor</h3>
            <p><code>constructor</code> 是构造方法，而<code>this</code>关键字则代表实例对象，对应 ES5 的构造函数，该方法在用<code>new</code>创建实例时自动调用该方法</p>
            <h3 id="extends-amp-super"><a href="#extends-amp-super" class="headerlink" title="extends &amp; super"></a>extends &amp; super</h3>
            <p><code>extends</code>继承一个类，<code>super</code>调用这个继承类的<code>constructor</code></p>
            <h3 id="取值函数（getter）和存值函数（setter）"><a href="#取值函数（getter）和存值函数（setter）" class="headerlink" title="取值函数（getter）和存值函数（setter）"></a>取值函数（getter）和存值函数（setter）</h3>
            <p>在“类”的内部可以使用<code>get</code>和<code>set</code>关键字，对某个属性设置存值函数和取值函数，拦截该属性的存取行为。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyClass</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">constructor</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">get</span> <span class="title">prop</span>() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;getter&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">set</span> <span class="title">prop</span>(<span class="params">value</span>) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;setter: &#x27;</span>+value);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> inst = <span class="keyword">new</span> MyClass();</span><br><span class="line"></span><br><span class="line">inst.prop = <span class="number">123</span>;</span><br><span class="line"><span class="comment">// setter: 123</span></span><br><span class="line"></span><br><span class="line">inst.prop</span><br><span class="line"><span class="comment">// &#x27;getter&#x27;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/class">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Modules（模块）"><a href="#Modules（模块）" class="headerlink" title="Modules（模块）"></a>Modules（模块）</h2>
            <p>ES6 引入了<code>import</code> <code>export</code>两个命令实现了ES6模块，用于取代<code>CommonJs</code>和<code>AMD</code>规范成为浏览器和服务器通用的解决方案。ES6 的模块自动采用严格模式，并且是编译时加载，所以<code>import</code>不能使用表达式和变量。</p>
            <p>接下来讲解一下这两个命令的一些用法，详细的说明可以查看更多信息</p>
            <h3 id="import"><a href="#import" class="headerlink" title="import"></a>import</h3>
            <p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import">import</a>的语法</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">import</span> defaultExport <span class="keyword">from</span> <span class="string">&quot;module-name&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> name <span class="keyword">from</span> <span class="string">&quot;module-name&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="keyword">export</span> &#125; <span class="keyword">from</span> <span class="string">&quot;module-name&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="keyword">export</span> <span class="keyword">as</span> alias &#125; <span class="keyword">from</span> <span class="string">&quot;module-name&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; export1 , export2 &#125; <span class="keyword">from</span> <span class="string">&quot;module-name&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; foo , bar &#125; <span class="keyword">from</span> <span class="string">&quot;module-name/path/to/specific/un-exported/file&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; export1 , export2 <span class="keyword">as</span> alias2 , [...] &#125; <span class="keyword">from</span> <span class="string">&quot;module-name&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> defaultExport, &#123; <span class="keyword">export</span> [ , [...] ] &#125; <span class="keyword">from</span> <span class="string">&quot;module-name&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> defaultExport, * <span class="keyword">as</span> name <span class="keyword">from</span> <span class="string">&quot;module-name&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;module-name&quot;</span>;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h3 id="export"><a href="#export" class="headerlink" title="export"></a>export</h3>
            <p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/export">export</a>的语法</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">export</span> &#123; name1, name2, …, nameN &#125;;</span><br><span class="line"><span class="keyword">export</span> &#123; variable1 <span class="keyword">as</span> name1, variable2 <span class="keyword">as</span> name2, …, nameN &#125;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">let</span> name1, name2, …, nameN; <span class="comment">// also var, const</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">let</span> name1 = …, name2 = …, …, nameN; <span class="comment">// also var, const</span></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">FunctionName</span>(<span class="params"></span>)</span>&#123;...&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">ClassName</span> </span>&#123;...&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> expression;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> (<span class="params">…</span>) </span>&#123; … &#125; <span class="comment">// also class, function*</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">name1</span>(<span class="params">…</span>) </span>&#123; … &#125; <span class="comment">// also class, function*</span></span><br><span class="line"><span class="keyword">export</span> &#123; name1 <span class="keyword">as</span> <span class="keyword">default</span>, … &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> * <span class="keyword">from</span> …;</span><br><span class="line"><span class="keyword">export</span> &#123; name1, name2, …, nameN &#125; <span class="keyword">from</span> …;</span><br><span class="line"><span class="keyword">export</span> &#123; import1 <span class="keyword">as</span> name1, import2 <span class="keyword">as</span> name2, …, nameN &#125; <span class="keyword">from</span> …;</span><br><span class="line"><span class="keyword">export</span> &#123; <span class="keyword">default</span> &#125; <span class="keyword">from</span> …;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/module">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Template-string（模板字符串）"><a href="#Template-string（模板字符串）" class="headerlink" title="Template string（模板字符串）"></a>Template string（模板字符串）</h2>
            <p>想当年我们拼接字符串大概也就是下面这几种方式：</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">// 短字符串拼接</span></span><br><span class="line"><span class="keyword">var</span> data = &#123;<span class="attr">msg</span>: <span class="string">&#x27;请求失败&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">var</span> msg = <span class="string">&#x27;msg: [&#x27;</span> + data.msg + <span class="string">&#x27;]&#x27;</span>;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">// 长字符串拼接（如HTML拼接）</span></span><br><span class="line"><span class="keyword">var</span> html = <span class="string">&#x27;&lt;body&gt;&#x27;</span></span><br><span class="line">              + <span class="string">&#x27;&lt;div&gt;&#x27;</span></span><br><span class="line">              <span class="comment">// ......</span></span><br><span class="line">              + <span class="string">&#x27;&lt;/div&gt;&#x27;</span></span><br><span class="line">            <span class="string">&#x27;&lt;/body&gt;&#x27;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">// 数组字符串拼接</span></span><br><span class="line"><span class="keyword">var</span> html = [</span><br><span class="line">  <span class="string">&#x27;&lt;body&gt;&#x27;</span>,</span><br><span class="line">  <span class="string">&#x27;&lt;div&gt;&#x27;</span>,</span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line">  <span class="string">&#x27;&lt;/div&gt;&#x27;</span>,</span><br><span class="line">  <span class="string">&#x27;&lt;/body&gt;&#x27;</span></span><br><span class="line">]</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>如果需要格式化的内容在字符串内会更麻烦，因此在千呼万唤中出现了模板字符串</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> data = &#123;<span class="attr">msg</span>: <span class="string">&#x27;请求失败&#x27;</span>&#125;</span><br><span class="line"><span class="keyword">const</span> html = <span class="string">`</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">  &lt;div class=&#x27;msg&#x27;&gt;</span></span><br><span class="line"><span class="string">    <span class="subst">$&#123;data.msg&#125;</span></span></span><br><span class="line"><span class="string">  &lt;/div&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">`</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>最后该字符串将按照这个格式进行输出</p>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/string#%E6%A8%A1%E6%9D%BF%E5%AD%97%E7%AC%A6%E4%B8%B2">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Default-parameters（默认参数）"><a href="#Default-parameters（默认参数）" class="headerlink" title="Default parameters（默认参数）"></a>Default parameters（默认参数）</h2>
            <p>ES6之前函数处理参数默认值是需要额外的代码的</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">log</span>(<span class="params">x, y</span>) </span>&#123;</span><br><span class="line">  y = y || <span class="string">&#x27;World&#x27;</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(x, y);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">log(<span class="string">&#x27;Hello&#x27;</span>) <span class="comment">// Hello World</span></span><br><span class="line">log(<span class="string">&#x27;Hello&#x27;</span>, <span class="string">&#x27;China&#x27;</span>) <span class="comment">// Hello China</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>这种方法<code>y</code>的值为<code>null</code> <code>undefined</code> <code>0</code> <code>false</code> 都会赋值为<code>World</code></p>
            <p>ES6之后可以使用更加简单的方法</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">log</span>(<span class="params">x, y = <span class="string">&#x27;World&#x27;</span></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(x, y)</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>这种方式是一种简单方法，但是只会判断<code>y</code>为<code>undefined</code>这一种情况</p>
            <p>通常会与解构赋值一起使用</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">log</span>(<span class="params">&#123;x, y = <span class="string">&#x27;World&#x27;</span>&#125;</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(x, y)</span><br><span class="line">&#125;</span><br><span class="line">log(&#123;<span class="attr">x</span>: <span class="string">&#x27;Hello&#x27;</span>&#125;) <span class="comment">// Hello World</span></span><br><span class="line">log(&#123;<span class="attr">x</span>: <span class="string">&#x27;Hello&#x27;</span>, <span class="attr">y</span>: <span class="string">&#x27;China&#x27;</span>&#125;) <span class="comment">// Hello China</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/function#%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0%E7%9A%84%E9%BB%98%E8%AE%A4%E5%80%BC">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="The-spread-operator（展开操作符）"><a href="#The-spread-operator（展开操作符）" class="headerlink" title="The spread operator（展开操作符）"></a>The spread operator（展开操作符）</h2>
            <p>扩展运算符（spread）是三个点（<code>...</code>）。它好比 rest 参数的逆运算，将一个数组转为用逗号分隔的参数序列。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="built_in">console</span>.log(<span class="number">1</span>, ...[<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>], <span class="number">5</span>)</span><br><span class="line"><span class="comment">// 1 2 3 4 5</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> numbers = [<span class="number">4</span>, <span class="number">38</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">push</span>(<span class="params">array, ...items</span>) </span>&#123;</span><br><span class="line">  array.push(...items);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">push([], ...numbers) <span class="comment">// [4, 38]</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <ul>
              <li>第三行是rest参数，将参数变成数组</li>
              <li>第四行是展开操作符，将数组变成参数</li>
              <li>第七行是展开操作符，将数组变成参数</li>
            </ul>
            <p>同时展开操作符也会对数组进行浅复制。</p>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/array#%E6%89%A9%E5%B1%95%E8%BF%90%E7%AE%97%E7%AC%A6">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Destructuring-assignments（解构赋值）"><a href="#Destructuring-assignments（解构赋值）" class="headerlink" title="Destructuring assignments（解构赋值）"></a>Destructuring assignments（解构赋值）</h2>
            <p>解构赋值常用的有对象解构、数组解构、对象数组嵌套解构、函数参数解构（基本就是前面三种情况的解构），当前还有字符串、数值、布尔值的解构。同时解构赋值也支持默认值。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">let</span> [a, b, c] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">let</span> [foo = <span class="literal">true</span>, [[bar], baz]] = [<span class="number">1</span>, [[<span class="number">2</span>], <span class="number">3</span>]];</span><br><span class="line"><span class="keyword">let</span> [x, y, ...z] = [<span class="string">&#x27;a&#x27;</span>];</span><br><span class="line"><span class="keyword">let</span> &#123; <span class="attr">foo</span>: foo1, <span class="attr">bar</span>: bar1 &#125; = &#123; <span class="attr">foo</span>: <span class="string">&#x27;aaa&#x27;</span>, <span class="attr">bar</span>: <span class="string">&#x27;bbb&#x27;</span> &#125;;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>比较重要的一点是当进行解构赋值的时候比较的是<code>undefined</code>，如果原本使用逻辑或设置默认值，这里需要注意下代码的逻辑是否合理。</p>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/destructuring">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Enhanced-object-literals（增强的对象字面量）"><a href="#Enhanced-object-literals（增强的对象字面量）" class="headerlink" title="Enhanced object literals（增强的对象字面量）"></a>Enhanced object literals（增强的对象字面量）</h2>
            <p>在ES6中对象进行了重大升级。</p>
            <h3 id="属性的简洁标识法"><a href="#属性的简洁标识法" class="headerlink" title="属性的简洁标识法"></a>属性的简洁标识法</h3>
            <p>你不再需要这么做</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> something = <span class="string">&#x27;y&#x27;</span></span><br><span class="line"><span class="keyword">const</span> x = &#123;</span><br><span class="line">  something: something</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>你只需：</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> something = <span class="string">&#x27;y&#x27;</span></span><br><span class="line"><span class="keyword">const</span> x = &#123;</span><br><span class="line">  something</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h3 id="属性名表达式"><a href="#属性名表达式" class="headerlink" title="属性名表达式"></a>属性名表达式</h3>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> x = &#123;</span><br><span class="line">  [<span class="string">&#x27;a&#x27;</span> + <span class="string">&#x27;_&#x27;</span> + <span class="string">&#x27;b&#x27;</span>]: <span class="string">&#x27;z&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">x.a_b <span class="comment">//z</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>这在ES6出现之前是不能办到的</p>
            <h3 id="super-关键字"><a href="#super-关键字" class="headerlink" title="super 关键字"></a>super 关键字</h3>
            <p>主要用在指向当前对象的原型对象</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> proto = &#123;</span><br><span class="line">  foo: <span class="string">&#x27;hello&#x27;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  foo: <span class="string">&#x27;world&#x27;</span>,</span><br><span class="line">  <span class="function"><span class="title">find</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">super</span>.foo;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.setPrototypeOf(obj, proto);</span><br><span class="line">obj.find() <span class="comment">// &quot;hello&quot;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>注意，<code>super</code>关键字表示原型对象时，只能用在对象的方法之中，用在其他地方都会报错。</p>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/object">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Generators"><a href="#Generators" class="headerlink" title="Generators"></a>Generators</h2>
            <p>Generator 函数是 ES6 提供的一种异步编程解决方案，语法行为与传统函数完全不同。</p>
            <h3 id="初步了解"><a href="#初步了解" class="headerlink" title="初步了解"></a>初步了解</h3>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">helloWorldGenerator</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">yield</span> <span class="string">&#x27;hello&#x27;</span>;</span><br><span class="line">  <span class="keyword">yield</span> <span class="string">&#x27;world&#x27;</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">&#x27;ending&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 代码初始化这个 Generator(生成器)函数</span></span><br><span class="line"><span class="keyword">var</span> hw = helloWorldGenerator();</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>上面代码定义了一个 Generator 函数<code>helloWorldGenerator</code>，它内部有两个<code>yield</code>表达式（<code>hello</code>和<code>world</code>），即该函数有三个状态：hello，world 和 return 语句（结束执行）。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="comment">// 第一次迭代启动迭代器</span></span><br><span class="line">hw.next()</span><br><span class="line"><span class="comment">// &#123; value: &#x27;hello&#x27;, done: false &#125;</span></span><br><span class="line"></span><br><span class="line">hw.next()</span><br><span class="line"><span class="comment">// &#123; value: &#x27;world&#x27;, done: false &#125;</span></span><br><span class="line"></span><br><span class="line">hw.next()</span><br><span class="line"><span class="comment">// &#123; value: &#x27;ending&#x27;, done: true &#125;</span></span><br><span class="line"></span><br><span class="line">hw.next()</span><br><span class="line"><span class="comment">// &#123; value: undefined, done: true &#125;</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>调用遍历器对象的<code>next</code>方法，使得指针移向下一个状态，直到<code>done</code>为<code>true</code>时停止。其中<code>yield</code>表达式是暂停执行的标记，而<code>next</code>方法可以恢复执行。</p>
            <p>总的来说Generator 函数是可以暂停的，并且能恢复执行。</p>
            <h3 id="一个更高级的例子"><a href="#一个更高级的例子" class="headerlink" title="一个更高级的例子"></a>一个更高级的例子</h3>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/generator">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">calculator</span>(<span class="params">input</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> doubleThat = <span class="number">2</span> * (<span class="keyword">yield</span> (input / <span class="number">2</span>))</span><br><span class="line">    <span class="keyword">var</span> another = <span class="keyword">yield</span> (doubleThat)</span><br><span class="line">    <span class="keyword">return</span> (input * doubleThat * another)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化这个 Generator(生成器)函数。注意这里不是用 new 。</span></span><br><span class="line"><span class="keyword">const</span> calc = calculator(<span class="number">10</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 第一次迭代启动迭代器。返回：&#123; value: 5, done: false &#125;</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 运行函数 input = 10 传递给生成器构造函数。</span></span><br><span class="line"><span class="comment"> * 它一直运行直到达到 yield，并返回 yield 的内容：input / 2 = 5。所以我们得到了一个值 5</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">calc.next()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在第二次迭代中，我们传递值 7。返回：&#123; value: 14, done: false &#125;</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 这个参数可以传入 Generator 函数，作为上个阶段异步任务的返回结果</span></span><br><span class="line"><span class="comment"> * 相当于 var doubleTaht = 2 * 7</span></span><br><span class="line"><span class="comment"> * 然后我们达到第二个 yield ，然后返回 doubleThat ，因此返回值为 14</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">calc.next(<span class="number">7</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在下一个和最后一个迭代中，我们传入 100。返回：&#123; value: 14000, done: true &#125;</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 本次迭代没有遇到yield，因此返回return的结果。同时 100 作为上个阶段异步任务的返回结果</span></span><br><span class="line"><span class="comment"> * 传入参数后相当于 var another = 100</span></span><br><span class="line"><span class="comment"> * 当迭代完成时我们只返回(input * doubleThat * another)，其数量为 10 * 14 * 100。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">calc.next(<span class="number">100</span>)</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h3 id="异步编程的应用"><a href="#异步编程的应用" class="headerlink" title="异步编程的应用"></a>异步编程的应用</h3>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/generator-async">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="for-of-循环"><a href="#for-of-循环" class="headerlink" title="for..of 循环"></a>for..of 循环</h2>
            <p>ES6 引入了<code>for...of</code>循环，作为遍历所有数据结构的统一的方法。</p>
            <p><code>for...of</code>循环可以使用的范围包括数组、Set 和 Map 结构、某些类似数组的对象、后文的 Generator 对象，以及字符串。</p>
            <p><code>for...of</code>可以使用<code>break</code> <code>continue</code> 关键字来终止循环和继续下一个循环</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> arr = [<span class="string">&#x27;red&#x27;</span>, <span class="string">&#x27;green&#x27;</span>, <span class="string">&#x27;blue&#x27;</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> v <span class="keyword">of</span> arr) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(v); <span class="comment">// red green blue</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">continue</span></span><br><span class="line"></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;--&#x27;</span>) <span class="comment">// 永远不会输出</span></span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>这里还会有一个数组循环的问题，查看<a target="_blank" rel="noopener" href="https://www.html.cn/archives/9261">JavaScript里的循环方法：forEach，for…in，for…of</a></p>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/iterator#for---of-%E5%BE%AA%E7%8E%AF">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h2 id="Map-和-Set"><a href="#Map-和-Set" class="headerlink" title="Map 和 Set"></a>Map 和 Set</h2>
            <p>ES6 提供了新的数据结构 Set。它类似于数组，但是成员的值都是唯一的，没有重复的值。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> s = <span class="keyword">new</span> <span class="built_in">Set</span>();</span><br><span class="line"></span><br><span class="line">[<span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">2</span>].forEach(<span class="function"><span class="params">x</span> =&gt;</span> s.add(x));</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i <span class="keyword">of</span> s) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(i);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 2 3 5 4</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>Set 结构的实例有以下属性。</p>
            <ul>
              <li>Set.prototype.constructor：构造函数，默认就是Set函数。</li>
              <li>Set.prototype.size：返回Set实例的成员总数。</li>
            </ul>
            <p>Set 实例的四个操作方法。</p>
            <ul>
              <li>add(value)：添加某个值，返回 Set 结构本身。</li>
              <li>delete(value)：删除某个值，返回一个布尔值，表示删除是否成功。</li>
              <li>has(value)：返回一个布尔值，表示该值是否为Set的成员。</li>
              <li>clear()：清除所有成员，没有返回值。</li>
            </ul>
            <p>Set 结构的实例有四个遍历方法，可以用于遍历成员。Set 的遍历顺序就是插入顺序。当然也可以使用<code>for....of</code> <code>...</code></p>
            <ul>
              <li>keys()：返回键名的遍历器</li>
              <li>values()：返回键值的遍历器</li>
              <li>entries()：返回键值对的遍历器</li>
              <li>forEach()：使用回调函数遍历每个成员</li>
            </ul>
            <p>同时还有WeakSet，WeakSet 的成员只能是对象，其次垃圾回收机制会自动回收该对象所占用的内存，不考虑该对象还存在于 WeakSet 之中</p>
            <p>ES6 引入Map结构来扩充键值对的集合，使各种类型的值都可以作为键值，是一种更完善的 Hash 结构表现。</p>
            <figure class="highlight js">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line"><span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> k1 = [<span class="string">&#x27;a&#x27;</span>];</span><br><span class="line"><span class="keyword">const</span> k2 = [<span class="string">&#x27;a&#x27;</span>];</span><br><span class="line"></span><br><span class="line">map</span><br><span class="line">.set(k1, <span class="number">111</span>)</span><br><span class="line">.set(k2, <span class="number">222</span>);</span><br><span class="line"></span><br><span class="line">map.get(k1) <span class="comment">// 111</span></span><br><span class="line">map.get(k2) <span class="comment">// 222</span></span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>同时也有WeakMap</p>
            <p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/#docs/set-map">&gt;&gt;查看更多信息&lt;&lt;</a></p>
            <h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1>
            <p><a target="_blank" rel="noopener" href="http://lzw.me/pages/ecmascript/">ECMAScript 5.1</a><br><a target="_blank" rel="noopener" href="https://www.html.cn/archives/9958">ES6 带来的重大特性 – JavaScript 完全手册（2018版）</a><br><a target="_blank" rel="noopener" href="http://www.ecma-international.org/ecma-262/5.1/">ecma-262-5.1</a><br><a target="_blank" rel="noopener" href="http://www.ecma-international.org/ecma-262/6.0/">ecma-262-6.0</a></p>

          </div>





          <footer class="post-footer">
            <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
              <a href="/tags/Ecma/" rel="tag"># Ecma</a>
              <a href="/tags/ECMAScript/" rel="tag"># ECMAScript</a>
              <a href="/tags/ES6/" rel="tag"># ES6</a>
              <a href="/tags/ES2015/" rel="tag"># ES2015</a>
              <a href="/tags/ECMAScript-2015/" rel="tag"># ECMAScript 2015</a>
            </div>



            <div class="post-nav">
              <div class="post-nav-item">
                <a href="/blob/2019-04-17-the-history-and-present-situation-of-javsscript/" rel="prev" title="JavsScript 发展史和现状">
                  <i class="fa fa-chevron-left"></i> JavsScript 发展史和现状
                </a>
              </div>
              <div class="post-nav-item">
                <a href="/blob/2019-04-17-new-features-of-javsscript-es7-&-es2016/" rel="next" title="JavsScript ES7&ES2016的新特性">
                  JavsScript ES7&ES2016的新特性 <i class="fa fa-chevron-right"></i>
                </a>
              </div>
            </div>
          </footer>
        </article>
      </div>







      <script>
        window.addEventListener('tabs:register', () => {
          let {
            activeClass
          } = CONFIG.comments;
          if (CONFIG.comments.storage) {
            activeClass = localStorage.getItem('comments_active') || activeClass;
          }
          if (activeClass) {
            const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
            if (activeTab) {
              activeTab.click();
            }
          }
        });
        if (CONFIG.comments.storage) {
          window.addEventListener('tabs:click', event => {
            if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
            const commentClass = event.target.classList[1];
            localStorage.setItem('comments_active', commentClass);
          });
        }
      </script>
    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


      <div class="copyright">
        &copy;
        <span itemprop="copyrightYear">2021</span>
        <span class="with-love">
          <i class="fa fa-heart"></i>
        </span>
        <span class="author" itemprop="copyrightHolder">holidaypenguin</span>
      </div>
      <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
      </div>

    </div>
  </footer>


  <script size="300" alpha="0.1" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
  <script src="/js/love.js"></script>

  <script src="/js/development.js"></script>
  <script src="/js/utils.js"></script>
  <script src="/js/motion.js"></script>
  <script src="/js/schemes/muse.js"></script>
  <script src="/js/next-boot.js"></script>


  <script src="/js/local-search.js"></script>









  <script>
    (function() {
      function leancloudSelector(url) {
        url = encodeURI(url);
        return document.getElementById(url).querySelector('.leancloud-visitors-count');
      }

      function addCount(Counter) {
        const visitors = document.querySelector('.leancloud_visitors');
        const url = decodeURI(visitors.id);
        const title = visitors.dataset.flagTitle;

        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({
            url
          })))
          .then(response => response.json())
          .then(({
            results
          }) => {
            if (results.length > 0) {
              const counter = results[0];
              leancloudSelector(url).innerText = counter.time + 1;
              Counter('put', '/classes/Counter/' + counter.objectId, {
                  time: {
                    '__op': 'Increment',
                    'amount': 1
                  }
                })
                .catch(error => {
                  console.error('Failed to save visitor count', error);
                });
            } else {
              Counter('post', '/classes/Counter', {
                  title,
                  url,
                  time: 1
                })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
            }
          })
          .catch(error => {
            console.error('LeanCloud Counter Error', error);
          });
      }

      function showTime(Counter) {
        const visitors = document.querySelectorAll('.leancloud_visitors');
        const entries = [...visitors].map(element => {
          return decodeURI(element.id);
        });

        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({
            url: {
              '$in': entries
            }
          })))
          .then(response => response.json())
          .then(({
            results
          }) => {
            for (let url of entries) {
              const target = results.find(item => item.url === url);
              leancloudSelector(url).innerText = target ? target.time : 0;
            }
          })
          .catch(error => {
            console.error('LeanCloud Counter Error', error);
          });
      }

      const {
        app_id,
        app_key,
        server_url
      } = {
        "enable": true,
        "app_id": "povuqNsSqFodlakVIwtEX5kb-gzGzoHsz",
        "app_key": "zXD40RDtDB3DMtpC89k0AK7g",
        "server_url": null,
        "security": false
      };

      function fetchData(api_server) {
        const Counter = (method, url, data) => {
          return fetch(`${api_server}/1.1${url}`, {
            method,
            headers: {
              'X-LC-Id': app_id,
              'X-LC-Key': app_key,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
          });
        };
        if (CONFIG.page.isPost) {
          if (CONFIG.hostname !== location.hostname) return;
          addCount(Counter);
        } else if (document.querySelectorAll('.post-title-link').length >= 1) {
          showTime(Counter);
        }
      }

      const api_server = app_id.slice(-9) === '-MdYXbMMI' ? `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com` : server_url;

      if (api_server) {
        fetchData(api_server);
      } else {
        fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
          .then(response => response.json())
          .then(({
            api_server
          }) => {
            fetchData('https://' + api_server);
          });
      }
    })();
  </script>



<script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(n){n.imageLazyLoadSetting.processImages=o;var e=n.imageLazyLoadSetting.isSPA,i=n.imageLazyLoadSetting.preloadRatio||1,r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]"));function o(){e&&(r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")));for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(n.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];t=o,e=function(){r=r.filter(function(t){return o!==t})},n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,e&&e()},t.src!==i&&(n.src=i)}()}o(),n.addEventListener("scroll",function(){var t,e;t=o,e=n,clearTimeout(t.tId),t.tId=setTimeout(function(){t.call(e)},500)})}(this);</script></body>

</html>